
SRCDIRS	:=.
			
INCDIRS :=. \
../../

TARGET := server

OUTPUT_DIR_NAME := obj
cross_complier :=
complier :=$(cross_complier)gcc
cpp_complier:=$(cross_complier)g++
LD :=$(cross_complier)ld
OBJCOPY :=$(cross_complier)objcopy
CFILES			:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c))
CPLUSPLUSFILES			:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.cpp))

SFILES :=$(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.S))

CSOUCEFILE_NODIR :=$(notdir $(CFILES))
SSOUCEFILE_NODIR :=$(notdir $(SFILES))
CPLUSPLUSSOUCEFILE_NODIR :=$(notdir $(CPLUSPLUSFILES))


COBJS :=$(CSOUCEFILE_NODIR:%.c=$(OUTPUT_DIR_NAME)/%.o)
CPLUSPLUSOBJS :=$(CPLUSPLUSSOUCEFILE_NODIR:%.cpp=$(OUTPUT_DIR_NAME)/%.o)
SOBJS :=$(SSOUCEFILE_NODIR:%.S=$(OUTPUT_DIR_NAME)/%.o)

OBJS := $(COBJS) $(SOBJS) $(CPLUSPLUSOBJS)
INCLUDE			:= $(patsubst %, -I%, $(INCDIRS))

C_DEFINES:=__HELLO__ 
LD_FLAGS =

CFLAGS :=$(patsubst %, -D%, $(C_DEFINES)) -Wall -std=c11 -O2
CPPFLAGS :=$(C_DEFINES) -Wall -std=c++11 -O2 

VPATH			:= $(SRCDIRS)
.PHONY:clean rebuild  debug all
#$(TARGET).bin:$(OBJS)
all:createDirs  $(TARGET)

$(TARGET):$(OBJS)
	#$(LD) -Timx6ul.lds -o $(OUTPUT_DIR_NAME)/$(TARGET).elf  $^
	#$(OBJCOPY) -O binary -S $(OUTPUT_DIR_NAME)/$(TARGET).elf $@
	$(complier)  $(LD_FLAGS) $^ -o $(OUTPUT_DIR_NAME)/$(TARGET)

$(OUTPUT_DIR_NAME)/%.o:%.c
	$(complier)	$(CFLAGS) $(INCLUDE) -c  $<  -o $@ 
$(OUTPUT_DIR_NAME)/%.o:%.S
	$(complier)  $(CFLAGS) $(INCLUDE)	-c $<   -o $@ 
$(OUTPUT_DIR_NAME)/%.o:%.cpp
	$(cpp_complier)  $(CPPFLAGS) $(INCLUDE)	-c $<  -o $@ 

createDirs:
	@mkdir -p $(OUTPUT_DIR_NAME)
		
clean:
	rm -rf $(OUTPUT_DIR_NAME) $(TARGET)
	
rebuild:clean $(TARGET).bin

debug:
	@echo $(CFILES)
	@echo $(SFILES)
	@echo $(CPLUSPLUSFILES)
	@echo $(CSOUCEFILE_NODIR)
	@echo $(SSOUCEFILE_NODIR)
	@echo $(CPLUSPLUSSOUCEFILE_NODIR)
	@echo $(OBJS)